using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 学生管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("student")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class StudentController : ControllerBase
    {
        private readonly IStudentService _studentService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="studentService">学生服务接口</param>
        public StudentController(IStudentService studentService)
        {
            _studentService = studentService;
        }

        /// <summary>
        /// 获取学生分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>学生分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<StudentDto>>> GetPagedList([FromBody] StudentPageRequest pageRequest)
        {
            var result = await _studentService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<StudentDto>>.Ok(result);
        }

        /// <summary>
        /// 获取学生详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>学生详情</returns>
        [HttpPost]
        public async Task<ApiResponse<StudentDto>> GetById([FromBody] IdRequest request)
        {
            var student = await _studentService.GetByIdAsync(request.Id);
            return ApiResponse<StudentDto>.Ok(student);
        }
        /// <summary>
        /// 根据学生IDs获取学生列表
        /// </summary>
        /// <returns>所有学生列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentDto>>> GetListByIds([FromBody] List<IdRequest> request)
        {
            var students = await _studentService.GetListByIdsAsync(request);
            return ApiResponse<List<StudentDto>>.Ok(students);
        }

        /// <summary>
        /// 创建学生
        /// </summary>
        /// <param name="input">创建学生请求参数</param>
        /// <returns>学生详情</returns>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<StudentDto>> Create([FromBody] CreateStudentDto input)
        {
            var student = await _studentService.CreateAsync(input);
            return ApiResponse<StudentDto>.Ok(student);
        }

        /// <summary>
        /// 更新学生
        /// </summary>
        /// <param name="input">更新学生请求参数</param>
        /// <returns>学生详情</returns>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<StudentDto>> Update([FromBody] UpdateStudentDto input)
        {
            var student = await _studentService.UpdateAsync(input);
            return ApiResponse<StudentDto>.Ok(student);
        }

        /// <summary>
        /// 删除学生
        /// </summary>
        /// <param name="request">ID请求参数</param>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _studentService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 获取学生编号最大值
        /// </summary>
        /// <returns>学生编号最大值</returns>
        [HttpPost]
        public async Task<ApiResponse<int>> GetMaxStudentNo()
        {
            var maxStudentNo = await _studentService.GetMaxStudentNoAsync();
            return ApiResponse<int>.Ok(maxStudentNo);
        }

        /// <summary>
        /// 根据课程ID获取学生分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数（包含课程ID）</param>
        /// <returns>报名了指定课程的学生分页列表</returns>
        //[HttpPost]
        //public async Task<ApiResponse<PagedResult<StudentDto>>> GetStudentsByCourse([FromBody] StudentPageRequest pageRequest)
        //{
        //    // 确保请求中包含课程ID
        //    if (!pageRequest.CourseId.HasValue)
        //    {
        //        return ApiResponse<PagedResult<StudentDto>>.Fail("课程ID不能为空");
        //    }

        //    var result = await _studentService.GetPagedListAsync(pageRequest);
        //    return ApiResponse<PagedResult<StudentDto>>.Ok(result);
        //}
    }
}